Imports PTT.CarRes.Data

Public Class ClassQueryInvOwner
    Public Function Add(ByVal InvOwner As QueryInvOwner, ByVal StrConn As String) As Boolean
        Dim ClsInvOwner As New ClassInvOwner
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvOwner.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvOwner
            .OwnerId = InvOwner.OwnerId
            .OwnerName = InvOwner.OwnerName
            .OwnerContactPerson = InvOwner.OwnerContactPerson
            .OwnerTelephone = InvOwner.OwnerTelephone
            .OwnerEmail = InvOwner.OwnerEmail
            .OwnerNo = InvOwner.OwnerNo
            .OwnerStreet = InvOwner.OwnerStreet
            .OwnerDistrict = InvOwner.OwnerDistrict
            .OwnerCity = InvOwner.OwnerCity
            .OwnerPotalCode = InvOwner.OwnerPotalCode
            .OwnerDescription = InvOwner.OwnerDescription
            .OwnerStatus = InvOwner.OwnerStatus
            .OfficeCode = InvOwner.OfficeCode
            .ProvinceId = InvOwner.ProvinceId
            ret = .USP_Save(Conn)
        End With
        ClsInvOwner.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Edit(ByVal InvOwner As QueryInvOwner, ByVal StrConn As String) As Boolean
        Dim ClsInvOwner As New ClassInvOwner
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvOwner.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvOwner
            .OwnerId = InvOwner.OwnerId
            .Load(Conn)
            .OwnerName = InvOwner.OwnerName
            .OwnerContactPerson = InvOwner.OwnerContactPerson
            .OwnerTelephone = InvOwner.OwnerTelephone
            .OwnerEmail = InvOwner.OwnerEmail
            .OwnerNo = InvOwner.OwnerNo
            .OwnerStreet = InvOwner.OwnerStreet
            .OwnerDistrict = InvOwner.OwnerDistrict
            .OwnerCity = InvOwner.OwnerCity
            .OwnerPotalCode = InvOwner.OwnerPotalCode
            .OwnerDescription = InvOwner.OwnerDescription
            .OwnerStatus = InvOwner.OwnerStatus
            .OfficeCode = InvOwner.OfficeCode
            .ProvinceId = InvOwner.ProvinceId
            ret = .USP_Update(Conn)
        End With
        ClsInvOwner.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Delete(ByVal InvOwner As QueryInvOwner, ByVal StrConn As String) As Boolean
        Dim ClsInvOwner As New ClassInvOwner
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvOwner.ConnectionOpen(StrConn)
        Dim ret As Boolean = False
        With ClsInvOwner
            .OwnerId = InvOwner.OwnerId
            .Load(Conn)
            ret = .USP_Delete(Conn)
        End With
        ClsInvOwner.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Search(Optional ByVal ParaOwnerId As String = "", _
    Optional ByVal ParaOwnerName As String = "", _
    Optional ByVal ParaOwnerStatus As String = "", _
    Optional ByVal ParaProvinceId As String = "", Optional ByVal StrConn As String = "") As QueryInvOwners
        Dim ClsInvOwner As New ClassInvOwner
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvOwner.ConnectionOpen(StrConn)
        Dim ret As QueryInvOwners = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvOwner"
        If ParaOwnerId & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(OwnerId = " & ParaOwnerId & ")"
        End If
        If ParaOwnerName & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(OwnerName like '%" & ParaOwnerName & "%')"
        End If
        If ParaOwnerStatus & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(OwnerStatus like '%" & ParaOwnerStatus & "%')"
        End If
        If ParaProvinceId & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(ProvinceId = " & ParaProvinceId & ")"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        End If
        Dim ds As DataSet
        With ClsInvOwner
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvOwners

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)

                            results.Add(New QueryInvOwner( _
                              .Item("OwnerId") & "", _
                              .Item("OwnerName") & "", _
                              .Item("OwnerContactPerson") & "", _
                              .Item("OwnerTelephone") & "", _
                              .Item("OwnerEmail") & "", _
                              .Item("OwnerNo") & "", _
                              .Item("OwnerStreet") & "", _
                              .Item("OwnerDistrict") & "", _
                              .Item("OwnerCity") & "", _
                              .Item("OwnerPotalCode") & "", _
                              .Item("OwnerDescription") & "", _
                              .Item("OwnerStatus") & "", _
                              .Item("OfficeCode") & "", _
                              .Item("ProvinceId") & "" _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvOwner.ConnectionClose(Conn)
        Return ret
    End Function
    Public Function Load(Optional ByVal ParaOwnerId As String = "", _
    Optional ByVal ParaOfficeCode As String = "", _
    Optional ByVal ParaProvinceId As String = "", Optional ByVal StrConn As String = "") As QueryInvOwners
        Dim ClsInvOwner As New ClassInvOwner
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvOwner.ConnectionOpen(StrConn)
        Dim ret As QueryInvOwners = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvOwner"
        If ParaOwnerId & "" <> "" Then
            If Condition & "" <> "" Then Condition &= " and "
            Condition &= "(OwnerId = " & ParaOwnerId & ")"
        End If
        If ParaOfficeCode & "" <> "" Then
            If Condition & "" <> "" Then Condition &= " and "
            Condition &= "(OfficeCode = '" & ParaOfficeCode & "')"
        End If
        If ParaProvinceId & "" <> "" Then
            If Condition & "" <> "" Then Condition &= " and "
            Condition &= "(ProvinceId = " & ParaProvinceId & ")"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        Else
            Return Nothing
            Exit Function
        End If
        Dim ds As DataSet
        With ClsInvOwner
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvOwners

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)

                            results.Add(New QueryInvOwner( _
                              .Item("OwnerId") & "", _
                              .Item("OwnerName") & "", _
                              .Item("OwnerContactPerson") & "", _
                              .Item("OwnerTelephone") & "", _
                              .Item("OwnerEmail") & "", _
                              .Item("OwnerNo") & "", _
                              .Item("OwnerStreet") & "", _
                              .Item("OwnerDistrict") & "", _
                              .Item("OwnerCity") & "", _
                              .Item("OwnerPotalCode") & "", _
                               .Item("OwnerDescription") & "", _
                              .Item("OwnerStatus") & "", _
                              .Item("OfficeCode") & "", _
                              .Item("ProvinceId") & "" _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvOwner.ConnectionClose(Conn)
        Return ret
    End Function
End Class